package com.java.controller;

import com.java.dto.RechargeDTO;
import com.java.model.*;
import com.java.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/front")
public class FrontIndexController {

    @Autowired
    private MediaAccountService mediaAccountService;

    @Autowired
    private PlatformService platformService;

    @Autowired
    private ProductService productService;

    @Autowired
    private AdvertisersAccountService advertisersAccountService;

    @Autowired
    private IndexSlideService indexSlideService;

    @GetMapping("/session")
    public String session(HttpServletRequest request, IndexSlide indexSlide, MediaAccount mediaAccount, Model model) {

        request.getSession().invalidate();
        Result list = indexSlideService.statusList(indexSlide);
        if (list.getCode().equals("0")) {
            model.addAttribute("list", list.getData());
        }

        Result result = mediaAccountService.resourceList(mediaAccount);
        if (list.getCode().equals("0")) {
            model.addAttribute("mediaAccount", result.getData());
        }

        Result result2 = mediaAccountService.resourceList2(mediaAccount);
        if (list.getCode().equals("0")) {
            model.addAttribute("mediaAccount2", result2.getData());
        }

        Result result3 = mediaAccountService.resourceList3(mediaAccount);
        if (list.getCode().equals("0")) {
            model.addAttribute("mediaAccount3", result3.getData());
        }

        Result result4 = mediaAccountService.resourceList4(mediaAccount);
        if (list.getCode().equals("0")) {
            model.addAttribute("mediaAccount4", result4.getData());
        }

        Result result5 = mediaAccountService.resourceList5(mediaAccount);
        if (list.getCode().equals("0")) {
            model.addAttribute("mediaAccount5", result5.getData());
        }
        return "/front/index";  // 被拦截器拦截
    }

    /**
     * 跳转前端首页
     * @return
     */
    @GetMapping("/frontIndex")
    public String frontIndex(IndexSlide indexSlide, MediaAccount mediaAccount, Model model) {

        Result list = indexSlideService.statusList(indexSlide);
        if (list.getCode().equals("0")) {
            model.addAttribute("list", list.getData());
        }

        Result result = mediaAccountService.resourceList(mediaAccount);
        if (list.getCode().equals("0")) {
            model.addAttribute("mediaAccount", result.getData());
        }

        Result result2 = mediaAccountService.resourceList2(mediaAccount);
        if (list.getCode().equals("0")) {
            model.addAttribute("mediaAccount2", result2.getData());
        }

        Result result3 = mediaAccountService.resourceList3(mediaAccount);
        if (list.getCode().equals("0")) {
            model.addAttribute("mediaAccount3", result3.getData());
        }

        Result result4 = mediaAccountService.resourceList4(mediaAccount);
        if (list.getCode().equals("0")) {
            model.addAttribute("mediaAccount4", result4.getData());
        }

        Result result5 = mediaAccountService.resourceList5(mediaAccount);
        if (list.getCode().equals("0")) {
            model.addAttribute("mediaAccount5", result5.getData());
        }
        return "/front/index";
    }

    /**
     * 跳转微信
     * @return
     */
    @GetMapping("/weChat")
    public String weChat(Model model) {
        List<MediaAccount> mediaAccount = mediaAccountService.saveList();
        model.addAttribute("mediaAccount", mediaAccount);

        return "/front/weChat";
    }

    /**
     * 根据自媒体帐号的名字搜索自媒体账号
     * @return
     */
    @PostMapping("/search")
    public String search(String accountName, Model model, MediaAccount mediaAccount) {

        List<MediaAccount> mediaAccounts = mediaAccountService.searchList(mediaAccount);
        model.addAttribute("mediaAccount", mediaAccounts);

        return "/front/weChat";
    }
    /**
     * 根据自媒体的所属平台搜索自媒体账号
     * @return
     */
    @GetMapping("/searchMicroblog")
    public String searchMicroblog(String platformName, Model model, MediaAccount mediaAccount) {

        List<MediaAccount> mediaAccounts = mediaAccountService.searchList(mediaAccount);
        model.addAttribute("mediaAccount", mediaAccounts);

        return "/front/weChat";
    }

    /**
     * 跳转登录
     * @return
     */
    @GetMapping("/frontLogin")
    public String frontLogin() {
        return "/front_page/login";
    }

    /**
     * 跳转注册
     * @return
     */
    @GetMapping("/frontReg")
    public String frontReg() {
        return "/front_page/reg";
    }

    /**
     * 跳转忘记密码
     * @return
     */
    @GetMapping("/frontGetpass")
    public String frontGetpass() {
        return "/front_page/getpass";
    }

    /**
     * SEO服务
     * @return
     */
    @GetMapping("/seoserve")
    public String seoserve() {
        return "/front/seoserve";
    }

    /**
     * 117了解
     * @return
     */
    @GetMapping("/consulting")
    public String consulting() {
        return "/front/consulting";
    }

    /**
     * 投稿
     * @return
     */
    @GetMapping("/contribute")
    public String contribute(Model model, HttpServletRequest request, int id, BigDecimal price, int priceId) {
        List<Platform> mediaAccount = platformService.toList();
        model.addAttribute("contribute", mediaAccount);

        List<Product> product = productService.toList();
        model.addAttribute("product",product);

        model.addAttribute("id", id);
        model.addAttribute("price", price);
        model.addAttribute("priceId", priceId);

        HttpSession session = request.getSession();
        session.setAttribute("priceId", priceId);
        return "/front/contribute";
    }

    /**
     * 个人中心
     * @return
     */
    @GetMapping("/topUp")
    public String topUp(int id, Model model, HttpServletRequest request) {
        AdvertisersAccount advertisersAccount = advertisersAccountService.walletList(id);
        model.addAttribute("advertisersAccount", advertisersAccount);

        AdvertisersAccount aa = (AdvertisersAccount) request.getSession().getAttribute("advertisersAccount");
        model.addAttribute("id", aa.getId());

        if (advertisersAccount.getAdvertisersWallet().getAvailableBalance() == null){//没有就新增一个账户
            AdvertisersWallet advertisersWallet = new AdvertisersWallet();
            //新建一个账户
            advertisersWallet.setAdsId(advertisersAccount.getId());
            advertisersAccountService.addRecharge(advertisersWallet);
        }

        return "/front/personalCenter";
    }
    /**
     * 充值金额
     * @return
     */
    @PostMapping("/addAmount")
    @ResponseBody
    public Result addAmount(RechargeDTO rechargeDTO) {

        //查询原来账号还有多少钱
        AdvertisersAccount recharge = advertisersAccountService.recharge(rechargeDTO.getId()); //此处问题为null前台值未传过来 2022:12.14 8:00  已解决 早上8：00

        // 原来的金额
        BigDecimal availableBalance = recharge.getAdvertisersWallet().getAvailableBalance();
        if (availableBalance.intValue() >= 0) {
            //充值的金额
            BigDecimal amount = rechargeDTO.getAvailableBalance();
            if (amount.intValue() > 0) {
                //把BigDecimal转换为int类型
                int i = availableBalance.intValue();
                int i2 = amount.intValue();
                //把原来的钱和现在的钱加起来
                int val = i + i2;
                //把int转换为BigDecimal类型
                BigDecimal bigDecimal = BigDecimal.valueOf(val);
                //新增以后修改金额
                AdvertisersWallet advertisersWallet = new AdvertisersWallet();
                advertisersWallet.setAvailableBalance(bigDecimal);
                advertisersWallet.setId(rechargeDTO.getId());

                return advertisersAccountService.money(advertisersWallet);
            } else {
                return Result.error("余额不能小于0！！！");
            }
        } else {
            return Result.error("出现错误！！！");
        }

    }

}
